import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    userId: '',
    userName: '',
    userSex: '',
    userOrigin: '',
    userClassName: '',
    userPartName: '',
    userLevel: '',
    userPhone: '',
    userEmail: '',
    userPwd: '',
    userStatus: '',
    userType: '',
    userOldStatus: '',
    avatar: ''

  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERID: (state, userId) => {
    state.userId = userId
  },
  SET_USERNAME: (state, userName) => {
    state.userName = userName
  },
  SET_USERSEX: (state, userSex) => {
    state.userSex = userSex
  },
  SET_USERORIGIN: (state, userOrigin) => {
    state.userOrigin = userOrigin
  },
  SET_USERCLASSNAME: (state, userClassName) => {
    state.userClassName = userClassName
  },
  SET_USERPARTNAME: (state, userPartName) => {
    state.userPartName = userPartName
  },
  SET_USERLEVEL: (state, userLevel) => {
    state.userLevel = userLevel
  },
  SET_USERPHONE: (state, userPhone) => {
    state.userPhone = userPhone
  },
  SET_USEREMAIL: (state, userEmail) => {
    state.userClassName = userEmail
  },
  SET_USERPWD: (state, userPwd) => {
    state.userPwd = userPwd
  },
  SET_USERSTATUS: (state, userStatus) => {
    state.userStatus = userStatus
  },
  SET_USERTYPE: (state, userType) => {
    state.userType = userType
  },
  SET_USEROLDSTATUS: (state, userOldStatus) => {
    state.userOldStatus = userOldStatus
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const data = response
        // 设置 token，作为用户已登陆的前端标识，存在 cookie 中
        commit('SET_TOKEN', data.obj)
        setToken(data.obj)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const data = response
        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        commit('SET_USERID', data.obj.userId)
        commit('SET_NAME', data.obj.userInfo.userName)
        commit('SET_USERNAME', data.obj.userInfo.userName)
        commit('SET_USERSEX', data.obj.userInfo.userSex)
        commit('SET_USERORIGIN', data.obj.userInfo.userOrigin)
        commit('SET_USERCLASSNAME', data.obj.userInfo.userClassName)
        commit('SET_USERPARTNAME', data.obj.userInfo.userPartName)
        commit('SET_USERLEVEL', data.obj.userInfo.userLevel)
        commit('SET_USERPHONE', data.obj.userInfo.userPhone)
        commit('SET_USEREMAIL', data.obj.userInfo.userEmail)
        commit('SET_USERPWD', data.obj.userPwd)
        commit('SET_USERSTATUS', data.obj.userStatus)
        commit('SET_USERTYPE', data.obj.userType)
        commit('SET_USEROLDSTATUS', data.obj.userOldStatus)
        commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

